package com.hyg.common.utils.glide;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.os.ParcelFileDescriptor;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import com.hyg.common.utils.SSLSocketClient;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;


@GlideModule
public class YRGlideAppMode extends AppGlideModule {

    public final OkHttpClient mClient = new OkHttpClient.Builder()
            .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
            .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
            .connectTimeout(15, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)//连接失败后是否重新连接
            .build();


    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {

        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(mClient));
//        .append(String.class, InputStream.class, new StringLoader.StreamFactory())
//                .append(String.class, ParcelFileDescriptor.class, new StringLoader.FileDescriptorFactory())
//                .append(
//                        String.class, AssetFileDescriptor.class, new StringLoader.AssetFileDescriptorFactory())
        registry.replace(String.class, InputStream.class, new WrapperStringLoader.StreamFactory());
        registry.replace(String.class, ParcelFileDescriptor.class, new WrapperStringLoader.FileDescriptorFactory());
        registry.replace(String.class, AssetFileDescriptor.class, new WrapperStringLoader.AssetFileDescriptorFactory());
        registry.prepend(String.class, InputStream.class,new UrlFactory());
    }

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        super.applyOptions(context, builder);
        //builder.setLogLevel(Log.DEBUG);
    }


    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }



}
